/**
 * 个人账单
 * @author  Jello
 * @create  2018/10/19 16:48
 * @version 1.0
 * @company 广东蓝聘科技网络有限公司
 **/
var sessionStorage = window.sessionStorage;
var spinner = new Spinner(opts);
var page = 1;
var size = 20;
var userid_sgin = "";

$(function() {
	userid_sgin = sessionStorage.getItem("userid_sign");
	if(!userid_sgin){
		userid_sgin = sessionStorage.getItem("openid");
	}

	backLogin(userid_sgin, "../../login.html", function() {
		//查询个人账单
		var param = {};
		param.page = page;
		param.userid_sgin = userid_sgin;
		queryPersonBill(param, function(data) {
			if(data != "" && data != null) {
				for(var i = 0; i < data.length; i++) {
					//循环后的数据要判断--billType,根据这个状态显示对应的数据
					if(data[i].billType == "1") {
						var type = data[i].type; //0其它1借支2管理费3工资4返费5注册获利7代扣借支8邀请奖励
						var temp = changeType(type);
						var data_html = '<div class="bill_box" onclick="otherBillDetail(' + type + ',' + data[i].money + ',' + data[i].time + ')">' +
							'<div class="bill_left"><span>' + temp + '</span>:+<span>' + data[i].money + '</span></div>' +
							'<div class="bill_right">' +
							'<div class="bill_rtop">' + timeFormat(data[i].time) + '</div>' +
							'<div class="bill_rbtm">已到账</div>' +
							'</div>' +
							'</div>';
						$(".per_bill").append(data_html);
					} else if(data[i].billType == "2") { //提现
						var temp = "";
						var type = data[i].type;
						if(type == 1) {
							temp = "处理中";
						} else if(type == 2) {
							temp = "已完成";
						}
						var data_html = '<div class="bill_box" onclick="withdrawBillDetail(' + data[i].id + ')">' +
							'<div class="bill_left"><span>账户提现</span>:-<span>' + data[i].money + '</span></div>' +
							'<div class="bill_right">' +
							'<div class="bill_rtop">' + timeFormat(data[i].time) + '</div>' +
							'<div class="bill_rbtm">' + temp + '</div>' +
							'</div>' +
							'</div>';
						$(".per_bill").append(data_html);
					}
				}
			} else {
				$(".zd_null").css("display", "block");
			}
		});
	})

	//屏幕滚动
	$(window).scroll(function() {
		var scrollTop = $(window).scrollTop(); //这个方法是当前滚动条滚动的距离
		var scrollHeight = $(window).height(); //获取当前窗体的高度   可見高度
		var windowHeight = $(document).height(); // 是获取整个页面的高度
		if(scrollTop >= windowHeight - scrollHeight) {
			flipPage();
		}
	});

})

/**
 * 翻页
 */
function flipPage() {
	page++;
	//查询个人账单
	var param = {};
	param.page = page;
	param.userid_sgin = userid_sgin;
	queryPersonBill(param, function(data) {
		for(var i = 0; i < data.length; i++) {
			//循环后的数据要判断--billType,根据这个状态显示对应的数据
			if(data[i].billType == "1") {
				var type = data[i].type; //0其它1借支2管理费3工资4返费5注册获利7代扣借支8邀请奖励
				var temp = changeType(type);
				var data_html = '<div class="bill_box" onclick="otherBillDetail(' + type + ',' + data[i].money + ',' + data[i].time + ')">' +
					'<div class="bill_left"><span>' + temp + '</span>:+<span>' + data[i].money + '</span></div>' +
					'<div class="bill_right">' +
					'<div class="bill_rtop">' + timeFormat(data[i].time) + '</div>' +
					'<div class="bill_rbtm">已到账</div>' +
					'</div>' +
					'</div>';
				$(".per_bill").append(data_html);
			} else if(data[i].billType == "2") { //提现
				var temp = "";
				var type = data[i].type;
				if(type == 1) {
					temp = "处理中";
				} else if(type == 2) {
					temp = "已完成";
				}
				var data_html = '<div class="bill_box" onclick="withdrawBillDetail(' + data[i].id + ')">' +
					'<div class="bill_left"><span>账户提现</span>:-<span>' + data[i].money + '</span></div>' +
					'<div class="bill_right">' +
					'<div class="bill_rtop">' + timeFormat(data[i].time) + '</div>' +
					'<div class="bill_rbtm">' + temp + '</div>' +
					'</div>' +
					'</div>';
				$(".per_bill").append(data_html);
			}
		}
		var length = data.length;
		endPage(length);
	});
}

function endPage(length) {
	if(length % size != 0 || length == 0) {
		setTimeout(function() {
			mui.toast('已全部加载');
		}, 600);
	}
}

/**
 * 其他账单详情
 * @param {Object} type 类型
 * @param {Object} money 金额
 * @param {Object} accountTime 到账时间
 */
function otherBillDetail(type, money, accountTime) {
	window.location.href = "otherbill_detail.html?type=" + type + "&money=" + money + "&time=" + accountTime;
}

function withdrawBillDetail(id) {
	window.location.href = "withdrawbill_detail.html?id=" + id;
}

/**
 * 转换类型
 * @param {Object} type 类型
 */
function changeType(type) {
	var temp = "";
	if(type == 0) {
		temp = "其它";
	} else if(type == 1) {
		temp = "借支到账";
	} else if(type == 2) {
		temp = "管理费";
	} else if(type == 3) {
		temp = "工资到账";
	} else if(type == 4) {
		temp = "返费到账";
	} else if(type == 5) {
		temp = "注册获利到账";
	} else if(type == 7) {
		temp = "代扣借支";
	} else if(type == 8) {
		temp = "邀请奖励到账";
	} else if(type == 9) {
		temp = "蓝聘红包";
	}
	return temp;
}

/**
 * 个人账单返回
 */
function billBack() {
	if(sessionStorage.getItem("wallet_sign") == 001166) {
		window.location.href = "../../wallet.html";
	} else {
		window.location.href = "../../app/personal/personal.html";
	}
}